package com.liveinstars.im.domain.manager.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liveinstars.im.domain.manager.UserOnlineInfoManager;
import com.liveinstars.api.im.query.UserOnlineInfoQuery;
import com.liveinstars.im.domain.dao.mapper.UserOnlineInfoMapper;
import com.liveinstars.im.domain.dao.model.UserOnlineInfo;
import com.onepiece.shipelves.common.constant.Constant;
import com.onepiece.shipelves.common.enums.SystemExceptionEnum;
import com.onepiece.shipelves.common.exception.DatabaseException;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 登录用户信息表 服务实现类
 * </p>
 *
 * @author 8btc
 * @since 2020-04-29
 */
@Service
public class UserOnlineInfoManagerImpl extends ServiceImpl<UserOnlineInfoMapper, UserOnlineInfo> implements UserOnlineInfoManager {

    private Logger logger = LoggerFactory.getLogger(UserOnlineInfoManagerImpl.class);

    @Override
    public void addOrUpdate(UserOnlineInfo userOnlineInfo) {
        try {
            userOnlineInfo.setCstCreated(LocalDateTime.now());
            userOnlineInfo.setCstModified(LocalDateTime.now());
            userOnlineInfo.setOnlineStatus(Constant.STATUS_DEFAULT);
            QueryWrapper<UserOnlineInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userOnlineInfo.getUserId())
                    .eq("client_id", userOnlineInfo.getClientId())
                    .eq("client_type", userOnlineInfo.getClientType());
            List<UserOnlineInfo> list = this.list(queryWrapper);
            if (CollectionUtils.isNotEmpty(list)) {
                UserOnlineInfo tmp = list.get(0);
                userOnlineInfo.setId(tmp.getId());
                userOnlineInfo.setCstCreated(tmp.getCstCreated());
            }
            this.saveOrUpdate(userOnlineInfo);
        } catch (Exception e) {
            logger.error("addOrUpdate params:{} errorMsg:{}",
                    "userOnlineInfo="+ JSON.toJSONString(userOnlineInfo), e.getMessage(), e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }

    @Override
    public List<UserOnlineInfo> listByQuery(UserOnlineInfoQuery query) {
        try {
            QueryWrapper<UserOnlineInfo> queryWrapper = new QueryWrapper<>();
            if (query.getChannelId() != null) {
                queryWrapper.eq("channel_id", query.getChannelId());
            }
            if (query.getChatServerId() != null) {
                queryWrapper.eq("chat_server_id", query.getChatServerId());
            }
            if (query.getUserId() != null) {
                queryWrapper.eq("user_id", query.getUserId());
            }
            List<UserOnlineInfo> list = this.list(queryWrapper);
            return list;
        } catch (Exception e) {
            logger.error("listByQuery params:{} errorMsg:{}",
                    "query="+ JSON.toJSONString(query), e.getMessage(), e);
            throw new DatabaseException(SystemExceptionEnum.MYSQL_OPERATING_ERROR);
        }
    }

    @Override
    public void deleteByChatServerIdAndChannelId(Long chatServerId, String channelId) {
        try {
            QueryWrapper<UserOnlineInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("chat_server_id", chatServerId)
                    .eq("channel_id", channelId);
            this.remove(queryWrapper);
        } catch (Exception e) {
            logger.error("deleteByChatServerIdAndChannelId params:{} errorMsg:{}",
                    "chatServerId="+ chatServerId + " channelId=" + channelId, e.getMessage(), e);
        }
    }

    @Override
    public void deleteByChatServerId(Long chatServerId) {
        try {
            QueryWrapper<UserOnlineInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("chat_server_id", chatServerId);
            this.remove(queryWrapper);
        } catch (Exception e) {
            logger.error("deleteByChatServerId params:{} errorMsg:{}",
                    "chatServerId="+ chatServerId, e.getMessage(), e);
        }
    }
}
